/**
 * 继承BaseScene类的一个示例;
 */
import {global} from '../Main';
import {modifyCameraControlMode} from '../Helpers';
import {BaseScene} from 'BaseScene';
export class MainScene extends BaseScene{
    /**
     * 只需要实现此方法，将playground的代码粘贴到此方法即可;
     * @returns
     */
    public iniScene(){
        //注意添加这两行，以匹配playground里的代码
        var engine = global.engine;
        var canvas = global.canvas;
        var scene = this.scene;
        
        var camera = new BABYLON.UniversalCamera("UniversalCamera", new BABYLON.Vector3(0, -8, -20), scene);
        //camera.setTarget(BABYLON.Vector3.Zero());
        modifyCameraControlMode(camera);    //修改相机的控制方式等
        camera.speed = 4;
        camera.inertia = 0; //惯性
        //camera.fov = 0.8;  //视角的弧度，视场
        //camera.angularSensibility = 2000;   //似乎与鼠标转向灵敏度有关

        // @ts-ignore   
        camera._needMoveForGravity = true;  //解决不移动的时候掉落停止的问题，此处报错忽略即可

        camera.attachControl(global.canvas, true);

        

        var actionManager = scene.actionManager = new BABYLON.ActionManager(scene);
        actionManager.registerAction(new BABYLON.ExecuteCodeAction(
            BABYLON.ActionManager.OnEveryFrameTrigger, 
            (evt) => {
                global.addMouseClickLockListner();
            }, new BABYLON.PredicateCondition(actionManager, () => {
                return !global.pointerLockListener;
            }))
        );

        // var meshMctionManager = doorL.actionManager = new BABYLON.ActionManager(scene);
        // meshMctionManager.registerAction(new BABYLON.ExecuteCodeAction(
        //     BABYLON.ActionManager.OnEveryFrameTrigger, 
        //     (evt) => {
        //         global.askForRender(SCENE.Demo1);
        //     }, 
        //     new BABYLON.PredicateCondition(meshMctionManager, () => {
        //         return false;
        //     }))
        // );


        // scene.afterRender = () => {
        //     if(doorL.getDistanceToCamera() < 3){
        //         global.removeMouseClickLockListner();
        //         global.askForRender(SCENE.Demo1);
        //         global.sceneToDrop = SCENE.MainScene;
        //     }
        // }
        var glbDemo = BABYLON.SceneLoader.Append("glb/", "chair.glb", scene, (scene) => {
            var mesh = scene.getMeshByID('立方体.002');
            mesh.position = new BABYLON.Vector3(5, -8.5, -10);
            mesh.checkCollisions = true;
        });
        

        // Lights
        var light0 = new BABYLON.DirectionalLight("Omni", new BABYLON.Vector3(-2, -5, 2), scene);
        var light1 = new BABYLON.PointLight("Omni", new BABYLON.Vector3(2, -5, -2), scene);

        //Ground
        var ground = BABYLON.Mesh.CreatePlane("ground", 40.0, scene);
        var groundMaterial = new BABYLON.StandardMaterial("groundMat", scene);
        // ground.material = new BABYLON.StandardMaterial("groundMat", scene);
        // ground.material.diffuseColor = new BABYLON.Color3(0, 0, 0);
        groundMaterial.specularColor = new BABYLON.Color3(0.23, 0.23, 0.23);
        groundMaterial.diffuseTexture = new BABYLON.Texture("https://minio.cnbabylon.com/babylon/p11/crate.png", scene);
        groundMaterial.backFaceCulling = false;
        ground.material = groundMaterial;
        ground.position = new BABYLON.Vector3(5, -10, -15);
        ground.rotation = new BABYLON.Vector3(Math.PI / 2, 0, 0);

        //Simple crate
        var box = BABYLON.Mesh.CreateBox("crate", 2, scene);
        var boxMaterial = new BABYLON.StandardMaterial("Mat", scene);
        // box.material = new BABYLON.StandardMaterial("Mat", scene);
        boxMaterial.diffuseTexture = new BABYLON.Texture("https://minio.cnbabylon.com/babylon/p11/crate.png", scene);
        boxMaterial.diffuseTexture.hasAlpha = true;
        box.material = boxMaterial;
        box.position = new BABYLON.Vector3(5, -9, -10);

        var doorL = BABYLON.MeshBuilder.CreateBox("doorL", { width: 0.1, height: 4, depth: 2 });
        var doorMaterial = new BABYLON.StandardMaterial("door", scene);
        doorMaterial.diffuseTexture = new BABYLON.Texture("https://minio.cnbabylon.com/babylon/p11/crate.png", scene);
        doorL.material = doorMaterial;
        doorL.position = new BABYLON.Vector3(5, -9, 0);
        


        //Simple crate 浮在空中
        var box1 = BABYLON.Mesh.CreateBox("crate", 2, scene);
        var box1Material = new BABYLON.StandardMaterial("Mat", scene);
        // box1.material = new BABYLON.StandardMaterial("Mat", scene);
        box1Material.diffuseTexture = new BABYLON.Texture("https://minio.cnbabylon.com/babylon/p11/crate.png", scene);
        box1Material.diffuseTexture.hasAlpha = true;
        box1.material  = box1Material;
        box1.position = new BABYLON.Vector3(5, 0, -10);

        //Set gravity for the scene (G force like, on Y-axis)
        scene.gravity = new BABYLON.Vector3(0, -0.1, 0);

        // Enable Collisions
        scene.collisionsEnabled = true;

        //Then apply collisions and gravity to the active camera
        camera.checkCollisions = true;
        camera.applyGravity = true;

        //Set the ellipsoid around the camera (e.g. your player's size)
        camera.ellipsoid = new BABYLON.Vector3(2, 1, 2);

        //finally, say which mesh will be collisionable
        ground.checkCollisions = true;
        box.checkCollisions = true;
    }

}
